<template>
  <div class="index">
    <div class="header-warpper" @click="$router.back(-1)">
      <img src="../../assets/l_jt.png" alt />
      <span>我的邀请码</span>
    </div>
    <div class="sm_content">
      <div class="r_code">
        <p>
          邀请码：
          <span class="idcode">{{yqm}}</span>
          <img class="copy" @click="copy1" src="../../assets/copyicon.png" alt />
        </p>
        <div id="qrcode"></div>
        <p>请扫描二维码获取链接地址</p>
      </div>
      <div class="address-box">
        <input type="text" id="address" readonly :value="tglj" placeholder="请先登录" />
        <button
          class="copy-btn"
          style="cursor: pointer;"
          data-clipboard-action="copy"
          data-clipboard-target="#address"
          @click="copy2"
        >复制链接</button>
      </div>
    </div>

    <!-- <Tabbar :idx="5" /> -->
  </div>
</template>

<script>
// @ is an alias to /src
// import Tabbar from "@/views/public/Tabbar.vue";
import QRCode from "qrcodejs2";
import Clipboard from "clipboard";
export default {
  name: "invite",
  components: {
    // Tabbar,
    QRCode
  },
  data() {
    return {
      yqm: "",
      tglj: ""
    };
  },
  created: function() {},
  mounted() {
    this.$axios
      .post("/index/apiindex/inviteUrl")
      .then(res => {
        if (res.data.code == 0) {
          this.yqm = res.data.info.invite_code;
          this.tglj = "http://"+window.location.host + "/dist/#/register?invite_code=" + this.yqm;
          this.$nextTick(function() {
            let qrcode = new QRCode("qrcode", {
              width: 142,
              height: 142, // 高度
              text: this.tglj // 二维码内容
            });
          });
        } else {
        }
      })
      .catch(function(error) {
        console.log(error);
      });
  },
  methods: {
    //  生成二维码
    copy1() {
      let text1 = this.yqm;
      var clipboard = new Clipboard(".copy", {
        text: function() {
          return text1;
        }
      });
      clipboard.on("success", e => {
        this.$createToast({
          txt: "复制成功",
          type: "txt"
        }).show();
        // 释放内存
        clipboard.destroy();
      });
      clipboard.on("error", e => {
        // 不支持复制
        this.$createToast({
          txt: "该浏览器不支持自动复制",
          type: "txt"
        }).show();
        // 释放内存
        clipboard.destroy();
      });
    },
    copy2() {
      let host = window.location.host;
      let text2 = "http://"+host + "/dist/#/register?invite_code=" + this.yqm;
      var clipboard = new Clipboard(".copy-btn", {
        text: function() {
          return text2;
        }
      });
      clipboard.on("success", e => {
        this.$createToast({
          txt: "复制成功",
          type: "txt"
        }).show();
        // 释放内存
        clipboard.destroy();
      });
      clipboard.on("error", e => {
        // 不支持复制
        this.$createToast({
          txt: "该浏览器不支持自动复制",
          type: "txt"
        }).show();
        // 释放内存
        clipboard.destroy();
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.sm_content {
  background: url("../../assets/invite.png") no-repeat;
  background-size: 100%;
  overflow: hidden;
}
.f-flex {
  display: flex;
  display: -webkit-flex;
  align-items: center;
}
.index {
  font-size: 0.4rem;
  .yqm {
    .f-flex();
    justify-content: space-between;
    margin: 0.533333rem auto 0.8rem;
    .ns1 {
      color: #737a92;
    }
    .ns2 {
      .f-flex();
      div {
        margin-left: 0.4rem;
        color: #e56337;
      }
    }
  }
  .tglj {
    p {
      text-align: left;
      color: #737a92;
    }
    .lj1 {
      text-align: left;
      word-break: break-all;
    }
    .yjfz {
      position: absolute;
      right: 0;
      color: #e56337;
      top: 0.4rem;
    }
  }
  .tgewm {
    .f-flex();
    justify-content: space-between;
    margin-top: 0.8rem;
  }
  .r_code {
    // .f-flex();
    // margin-top: 0.4rem;
    // justify-content: center;
  }
}

.r_code {
    width: 89%;
    margin: 5.6rem auto 0;
    border-radius: 16px;
    padding: 0.6rem 0 0.4rem;
    text-align: center;
    background: #fff;
  p {
    margin-bottom: 0;
    font-size: 0.46rem;
  }
  .copy {
    width: 0.45rem;
    position: relative;
    margin-left: 0.2rem;
    top: -0.1rem;
  }
}
#qrcode{
  text-align: center;
    display: flex;
    justify-content: center;
     margin: 0.2rem 0;
}

.address-box {
  margin: 0 auto;
  margin-top: 0px;
  margin-top: 0.533333rem;
  width: 8.186667rem;
}
#address {
  display: block;
  padding: 0.24rem 0.4rem;
  width: 100%;
  height: 0.906667rem;
  line-height: 0.426667rem;
  font-size: 0.32rem;
  color: #42454e;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: none;
}
.copy-btn {
  display: block;
  margin-top: 0.533333rem;
  width: 100%;
  height: 1.173333rem;
  line-height: 1.173333rem;
  font-size: 0.48rem;
  color: #fff;
  text-align: center;
  border: none;
  border-radius: 0.266667rem;
  background: #d63500;
  cursor: pointer;
}
</style>

